#include "../include/MainHeader.h"

int mountVfs(char *vfs_label)
{
	if(ismounted==1)
		return 3;


	nBlocks=0;
	char label[50];
	naryRoot=NULL;
	bstRoot=NULL;
	hash=NULL;
	FILE *f;

	
	f=fopen(vfs_label,"rb+");	
	if(f==NULL)
	{
		return 1;
	}

	else
	{ 
		


	//header.noOfUsedFileDescriptor=fdcounter;

			int b;
			long int nodesc;
			char c='a',*s="empty1",testflag;
			int j,i,bi;
			offset=0;
	
	
	
			fd test;
			strcpy(test.file_name,"ss");

			
			fread(&header,sizeof(header),1,f);
			


		//	fseek(f,60,SEEK_SET);	
			
			if(fread(&testflag,sizeof(char),1,f)==0)
				return 2;

			//printf("testchar=%c\n",testflag );

			//fseek(f,offset,SEEK_SET);	
			//fread(label,(50*sizeof(char)),1,f);
			//strcpy(header.fileSystemLabel,label);
			//printf("Header flabel=%s",header.fileSystemLabel);

			// calculating number of blocks


			fseek(f,offset,SEEK_SET);	
			while(c!='@')
			{
				fread(&c,sizeof(c),1,f);
				offset++;
		
			}
	
			fseek(f,offset,SEEK_SET);
			fread(&nBlocks,sizeof(nBlocks),1,f);

			//printf("no blocks=%ld\n",nBlocks);
			header.maxNoOfFileDescriptor=nBlocks;
	
			// finding offset for FD Array
			offset=0;
			fseek(f,offset,SEEK_SET);
			while(c!='#')
			{
		
				fread(&c,sizeof(c),1,f);
				offset++;
		
			}
			fseek(f,offset,SEEK_SET);

	
			//to find number of valid file descriptors	
			b=-1;
		 
		
			/*while((strcmp(test.file_name,s))!= 0){
	
			fread(&test,sizeof(struct FileDescriptor),1,f);
			b++;

			}
	
	
			header.noOfUsedFileDescriptor=b;*/
			b=header.noOfUsedFileDescriptor;
	
			//printf("usedfd=%d\n",b );
			//printf("\nValue of b=%d\n",b);	
		//	printf("\nValid File descriptors =%d\n",header.noOfUsedFileDescriptor);
	
			struct FileDescriptor fileDesc[b];

			//printf("offset=%ld\n",offset);

		//	printf("sizesss===%d\n",sizeof(struct FileDescriptor));

			struct FileDescriptor arrayfd[nBlocks];
			fread(arrayfd,sizeof(fd),nBlocks,f);
		//	printf("%s",arrayfd[0].file_name);

			

			// Populating Nary BST 
			for(j=0;j<header.noOfUsedFileDescriptor;j++)
			{
				//printf("\nIn for insert while mount\n");
		//	printf("\narrayfd before insert nary=%s\n",arrayfd[j].file_name);
				i=insert_node(&naryRoot,&arrayfd[j]);
				fdcounter--;
				bi=bstInsertNode(&bstRoot,&arrayfd[j]);

				if(j!=0){

				strcpy(fileDesc[j-1].file_name,arrayfd[j].file_name);
				strcpy(fileDesc[j-1].path, arrayfd[j].path);

				}

			}



		//	printtree(naryRoot);
			 
			//Populating Hash Table

			hash=(struct HashTable*)malloc (sizeof(struct HashTable));
			loadHashTable(fileDesc,hash,(b-1));
	
	}

//printf("\n-------------------End of Mount-------------\n");
	

	fclose(f);

ismounted=1;



return 0;

}






	//fd4.file_name="file4";
	//hashSearch(hash,fd4);
	//hashDelete(hash,fd4)*/


	/*


	fseek(f,offset,SEEK_SET);
	struct FileDescriptor arrayfd2[nBlocks];
	fread(arrayfd2,sizeof(struct FileDescriptor),nBlocks,f);


	int d;	

	for(d=0;d<7;d++){
		struct FileDescriptor *test1=(struct FileDescriptor*) malloc(sizeof(struct FileDescriptor));
		test1=&arrayfd2[d];
		
		printf("fd1 after mounting=%s\n",test1->file_name);
		printf("fd1 path after mounting=%s\n",test1->path);
		
	}



	//fseek(f,offset,SEEK_SET);

	*/

